package org.ypx.center.controller.impl;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.ypx.center.controller.TaskController;
import org.ypx.center.service.ITaskService;
import org.ypx.db.entity.Task;
import org.ypx.db.entity.User;
import org.ypx.db.entity.UserInfo;
import org.ypx.db.entity.UserTask;
import org.ypx.shared.common.Result;
import org.ypx.shared.service.vo.TaskVO;
import org.ypx.shared.util.BeanUtil;

import java.sql.Date;
import java.util.List;

/**
 * @author tingwen
 * @date 2017/3/28 21:06
 * @description
 */
@Controller
@RequestMapping("task")
public class TaskControllerImpl extends BaseControllerImpl<Task> implements TaskController {
    @Autowired
    private ITaskService taskService;

    /*
    * notice */
    @RequestMapping("selectByUserTask.do")
    @ResponseBody
    @Override
    public Result<List<Task>> selectByUserTask(UserTask userTask) {
        return taskService.selectByUserTask(userTask);
    }

    /*
    * taskList.html*/
    @RequestMapping("selectTaskList.do")
    @ResponseBody
    public Result<List<TaskVO>> selectTaskList(Task task) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getSession().getAttribute("user");
        if (subject.hasRole("student")) {
            UserTask userTask = new UserTask();
            userTask.setUserId(user.getUserId());
            return taskService.selectTaskList(task, userTask);
        }
        return taskService.selectTaskList(task);
    }

    @RequestMapping("saveByUserInfo.do")
    @ResponseBody
    public Result<List<Task>> saveByUserInfo(Task entity, String startTimeProxy, String endTimeProxy, UserInfo userInfo) {
        entity.setStartTime(Date.valueOf(startTimeProxy));
        entity.setEndTime(Date.valueOf(endTimeProxy));
        userInfo = (UserInfo) BeanUtil.initBean(userInfo);
        return taskService.saveByUserInfo(entity, userInfo);
    }

}
